package edu.aynu.controller;

import edu.aynu.entity.NewUser;
import edu.aynu.entity.User;
import edu.aynu.entity.User_Role;
import edu.aynu.service.EmailService;
import edu.aynu.service.UserRoleService;
import edu.aynu.service.UserService;
import edu.aynu.util.CodeUtil;
import edu.aynu.util.Result;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * 登录的信息接口
 *   直接放行
 */
@RestController
@RequestMapping("/login")
public class LoginController {

    @Resource
    private UserService userService;

    @Resource
    private UserRoleService userRoleService;

    @Resource
    private EmailService emailService;

    @Resource
    private PasswordEncoder passwordEncoder;

    /**
     * * 判定密码是否正确的登录直接请求   /login
     * 当去掉权限控制默认的登录方式的时候可以使用此接口，进行密码账号的判定，
     * 登录的时候账号或者邮箱与密码判定
     * @RequestBody
     * @param user
     * @return
     */
    @PostMapping("/password")
    public Result login(@RequestBody User user){
        User user1 = userService.queryOne(user);
        System.out.println(user1);
        if (user1 == null){
            return Result.fail().add("tips","查无此人，请先注册");
        }
        if (!passwordEncoder.matches(user.getPassword(), user1.getPassword())){
            return Result.fail().add("tips","密码错误，请重新输入");
        }
        if(user1.getActivation() != 1){
            return Result.fail().add("tips","该账户未激活，" +
                    "请联系管理员激活。激活方式请发送‘账号+邮箱+密码’到‘**********@qq.com’");
        }
        return Result.success().add("user",user1);
    }

    /**
     * 判断邮箱或者账号是否已经被使用
     * @param user
     * @return
     */
    @PostMapping("/deterUser")
    public Result deterEmail(@RequestBody User user){
        User user1 = userService.deterUsername(user.getUsername());
        if (user1 != null){
            return Result.fail().add("tips","账号已经被使用，请更换账号");
        }
        User user2 = userService.deterEmail(user.getEmail());
        if (user2 != null){
            return Result.fail().add("tips","邮箱已经被注册，更换邮箱之后，在注册");
        }
        return Result.success().add("tips","检验通过");
    }

    /**
     * 注册的新用户，使用邮箱发送验证码
     * 发送邮件
     * @param newUser
     * @return
     */
    @PostMapping("/register/email")
    public Result registerUser(@RequestBody NewUser newUser){
        Boolean aBoolean = emailService.sendEmail(newUser.getEmail(), CodeUtil.getCode());
        if (aBoolean){
            return Result.success().add("tips","邮件已经发送");
        }else {
            return Result.fail().add("tips","已经往邮箱里面发送过验证码，验证码过期时间为5分钟，请勿多次发送");
        }
    }

    /**
     * 使用  验证码判定登录
     * 登录成功之后，初始化密码都为   123
     * @param newUser
     * @return
     */
    @PostMapping("/code")
    public Result loginCode(@RequestBody NewUser newUser){
        Object o = emailService.getCodeRedis(newUser.getEmail());
        if (o == null){
            return Result.fail().add("tips","验证码已经过期");
        }
        if (!newUser.getCode().equals(o)){
            return Result.fail().add("tips","您输入的验证码错误");
        }
        User user = new User(newUser.getEmail(), newUser.getUsername(), newUser.getPassword());
        int i = userService.addUser(user);

        User user1 = userService.deterEmail(user.getEmail());
        userRoleService.addRole(new User_Role(user1.getId(),2));

        if (i > 0){
            return Result.success().add("tips","恭喜您已经成功涛帅考试用户");
        }else {
            return Result.fail().add("tips","插入数据失败");
        }
    }

    @PostMapping("/registerUsername")
    public Result registerUsername(@RequestBody NewUser newUser){

        User user = new User(newUser.getUsername(), newUser.getPassword());
        int i = userService.addUser(user);

        //插入权限
        User user1 = userService.deterUsername(user.getUsername());
        userRoleService.addRole(new User_Role(user1.getId(),2));
        if (i > 0){
            return Result.success().add("tips","恭喜您已经成功涛帅考试用户");
        }else {
            return Result.fail().add("tips","插入数据失败");
        }
    }

}
